package com.example.myfoodrecipe.data.local

import android.content.Context
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.myfoodrecipe.data.local.entity.FavoriteEntity
import com.example.myfoodrecipe.data.local.entity.RecipeEntity
import com.example.myfoodrecipe.data.model.Result
import kotlinx.coroutines.flow.Flow

//数据仓库：实现数据的存取等
class LocalRepository(context: Context) {
    private val recipeDao = RecipeDatabase.getDatabase(context).getRecipeDao()

    //插入数据,重复则替换
    suspend fun insertRecipe(recipeEntity: RecipeEntity){
        recipeDao.insertRecipe(recipeEntity)
    }

    //查询数据
    fun getRecipes(type: String): Flow<List<RecipeEntity>> {
        return recipeDao.getRecipes(type)
    }

    //更新数据
    suspend fun updateRecipe(recipeEntity: RecipeEntity){
        recipeDao.updateRecipe(recipeEntity)
    }

    /**--------------favorite-------------*/
    //点击favFragment获取所有收藏
    fun getAllFav(): Flow<List<FavoriteEntity>>{
        return recipeDao.getAllFav()
    }

    //插入收藏食谱
    suspend fun insertFav(favoriteEntity: FavoriteEntity){
        recipeDao.insertFav(favoriteEntity)
    }

    //删除收藏食谱
    suspend fun deleteFav(favoriteEntity: FavoriteEntity){
        recipeDao.deleteFav(favoriteEntity)
    }
}
